#include "sdk.h"
#include <wx/filesys.h>
#include "ClassWizard.h"
#include "classwizarddlg.h"

namespace {
  PluginRegistrant<ClassWizard> reg( _T( "ClassWizard" ) );
  int idLaunch = XRCID( "idFileNewClass" );
}

BEGIN_EVENT_TABLE( ClassWizard, M_Code_Plugin )
  EVT_MENU( idLaunch, ClassWizard::OnLaunch )
END_EVENT_TABLE()

ClassWizard::ClassWizard() {
  if( !Manager::LoadResource( _T( "ClassWizard.zip" ) ) ) {
    NotifyMissingFile( _T( "ClassWizard.zip" ) );
  }
}

ClassWizard::~ClassWizard() {
}

void ClassWizard::OnAttach() {
  M_Code_Plugin::OnAttach();
}

void ClassWizard::OnRelease( bool appShutDown ) {
  M_Code_Plugin::OnRelease( appShutDown );
}

void ClassWizard::OnLaunch( M_Code_unused wxCommandEvent& event ) {
  ProjectManager* prjMan = Manager::Get()->GetProjectManager();
  M_Code_Project* prj = prjMan->GetActiveProject();
  ClassWizardDlg dlg( Manager::Get()->GetAppWindow() );
  PlaceWindow( &dlg );
  if( dlg.ShowModal() == wxID_OK ) {
    if( !prj ) {
      msging( _T( "新类已创建." ), _T( "信息" ), wxOK | wxICON_INFORMATION, Manager::Get()->GetAppWindow() );
    } else if( msging( _T( "新类已创建 是否将其添加到当前项目?" ), _T( "添加到项目?" ), wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION, Manager::Get()->GetAppWindow() ) == wxID_YES ) {
      wxArrayInt targets;
      prjMan->AddFileToProject( dlg.GetHeaderFilename(), prj, targets );
      if( ( targets.GetCount() != 0 ) && ( dlg.IsValidImplementationFilename() ) ) {
        prjMan->AddFileToProject( dlg.GetImplementationFilename(), prj, targets );
      }
      if( dlg.AddPathToProject() ) {
        for( size_t i = 0; i < targets.GetCount(); ++i ) {
          ProjectBuildTarget* buildTarget = prj->GetBuildTarget( targets[i] );
          if( buildTarget ) {
            wxString include_dir = dlg.GetIncludeDir();
            if( !include_dir.IsEmpty() )
            { buildTarget->AddIncludeDir( include_dir ); }
          } else {
            wxString information;
            information.Printf( _T( "找不到生成目标ID = %i.\n不会将包含目录添加到此目标 请手动操作" ), targets[i] );
            msging( information, _T( "信息" ), wxOK | wxICON_INFORMATION, Manager::Get()->GetAppWindow() );
          }
        }
      }
      prjMan->GetUI().RebuildTree();
    }
  }
}
